using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GuiScript : MonoBehaviour{
	
	#region texturas
	public GUIStyle[] guiStyles;
	#endregion
	
	private Rect[] windows;
	private bool[] IsPressed;
	private bool[] IsWndActive;
	private bool[] IsToggleActive;
	
	public GUISkin guiSkin;
	
	#region cores usadas
	private float widthGUIDivisor = 6.3f;
	private Color corSelecionado 	= new Color(1.0F,0.0F,0.0F); 
	private Color corNaoSelecionado = new Color(1.0F,1.0F,1.0F); 
	#endregion
	
	#region texturas
	public Texture mainTexture;
	public Texture infoTexture;
	public Texture[] camDirectionTextures;
	#endregion
	
	#region aumentar e diminuir o tamanho do mundo
	private float vSlider = 60F;
	private float maxSlider = 90F;
	private float minSlider = 30F;
	#endregion
	
	internal float STEP  = 15.0F;
	internal float ANGLE = 2F;
	internal float maxHeight = 20F;
				
	void Start(){
		
		IsPressed  = new bool[10];
		IsWndActive = new bool[10];
		IsToggleActive = new bool[10];
		windows = new Rect[10];
		
		//Inicializando as janelas
		windows[0] = new Rect(100,100,300,300);
	}
	
	void Update(){
		
		this.GetComponent<Camera>().fieldOfView = maxSlider + minSlider - vSlider;
		
		//Se clicar fora do GUI da esquerda verificar o móvel selecionado
		if(Input.mousePosition.x > (Screen.width / widthGUIDivisor))
			CheckActiveFurniture();
		
	}
	
	void OnGUI(){
		
		GameObject movelSelecionado = GameObject.FindGameObjectWithTag("MovelSelecionado");
		
		GUI.skin = guiSkin;
		
		//Se houver um movel selecionado pintá-lo de alguma cor
		if(movelSelecionado != null){
			movelSelecionado.renderer.material.color = corSelecionado;
		}		
		
		DrawLeftGUI();
		DrawActiveFurnitureBox();
		
		if(IsWndActive[0]){
        	windows[0] = GUI.Window(0, windows[0], DrawWindow, "My First Window");
		}
	}
	
	private void DrawLeftGUI(){
		GUI.BeginGroup(new Rect(0,0,Screen.height,Screen.width));
		
			//Desenhar background
			GUI.DrawTexture(ScreenUtils.ScaledRect(0.0f,0.0f,0.155f,1f),mainTexture);
		
			//Mover a camera
			DrawLookCameraControl();
			DrawMoveCameraControl();
				
			//Botão da interface
			IsPressed[9] = GUI.Button(new Rect(Screen.width  / 12f,
		                                   	   Screen.height / 1.105f,
		                                       Screen.height / 18f,
		                                       Screen.height / 18f), infoTexture);
			if(IsPressed[9]){
				IsWndActive[0] = !IsWndActive[0];
			}
		
			//Slider
			vSlider = GUI.HorizontalSlider(new Rect(Screen.width  / 25.5f,
			                                   	    Screen.height / 1.735f,
			                                        Screen.width / 13.5f,
			                                        Screen.height / 18f),
		                               				vSlider,minSlider,maxSlider);
		
		GUI.EndGroup();	
	}
	
	private void CheckActiveFurniture(){
		
		GameObject movelSelecionado = GameObject.FindGameObjectWithTag("MovelSelecionado");
		
		//Se houver um movel selecionado pintá-lo de alguma cor
		if(movelSelecionado != null){
			movelSelecionado.renderer.material.color = corSelecionado;
		}
		
		//Botão esquerdo
		if(Input.GetMouseButtonDown(0)){
			
			//Somente fazer testes de seleção e deseleção se o mouse não forem usados
			
			//Deselecionar móvel selecionado se clicar com o botão esquerdo
			if(movelSelecionado != null){
				movelSelecionado.tag = "Movel";
       			movelSelecionado.renderer.material.color = corNaoSelecionado;
			}
			
			RaycastHit hit = new RaycastHit();
			Camera camera = GameObject.FindGameObjectWithTag("Player").GetComponent<Camera>();
			Ray ray =  camera.camera.ScreenPointToRay(Input.mousePosition);
			GameObject[] moveis = GameObject.FindGameObjectsWithTag("Movel");
			
			//Só continua se pegar algum móvel no ray cast do mouse
					
			//Se houverem moveis na cena 
			if(moveis.Length > 0){
				foreach (GameObject movel in moveis){
					if(Physics.Raycast(ray, out hit, 100.0f))
					   if(hit.transform == movel.transform){
							movel.tag = "MovelSelecionado";
	   						movel.renderer.material.color = corSelecionado;
							break;
						}
				}
			} else {
				print("Não há mais objetos deselecionados.");
			}
		}
	}

	private void DrawWindow(int windowID){
		switch(windowID){
			case 0:
				IsToggleActive[0] = GUI.Toggle(new Rect(10, 20, 50, 50),IsToggleActive[0], "Hello World");
				IsWndActive[0] 	  = !GUI.Button(new Rect(200, 20, 50, 50), "Exit");
				GUI.DragWindow();
				break;
			default:
				break;
		}
	}
	
	private Rect windowActiveFurniture;
	private void DrawActiveFurnitureBox(){
		
		GameObject movelSelecionado = GameObject.FindGameObjectWithTag("MovelSelecionado");
		if(movelSelecionado != null){
			if(IsWndActive[1] == false){
				windowActiveFurniture = new Rect(Event.current.mousePosition.x - ScreenUtils.ScaleWithHeight(0.25F),
				                                 Event.current.mousePosition.y - ScreenUtils.ScaleWithWidth(0.25F), 
				                                 ScreenUtils.ScaleWithWidth(0.25F),
				                                 ScreenUtils.ScaleWithHeight(0.25F));
				IsWndActive[1] = true;	
			}
		} else {
			IsWndActive[1] = false;	
		}
		
		if(IsWndActive[1]){
        	windows[1] = GUI.Window(1, windowActiveFurniture, DrawWindow, "Opções");
		}
	}
	
	private void OnControllerColliderHit(ControllerColliderHit hit){
		if(hit.gameObject.tag != "Chao" && hit.gameObject.tag != "Parede"){
	 		Physics.IgnoreCollision(this.collider,hit.collider,true);
		}
	}
	
	void DrawLookCameraControl(){
		
		//Look up button
		if(GUI.RepeatButton(new Rect(Screen.width  / 16f,
	                           	     Screen.height / 8.5f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[0])
		   ){
			transform.RotateAroundLocal(transform.right, - ANGLE * Time.deltaTime);
		}
		
		//Look down button
		if(GUI.RepeatButton(new Rect(Screen.width  / 16f,
	                           	     Screen.height / 3.95f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[2])
		   ){
			transform.RotateAroundLocal(transform.right, ANGLE * Time.deltaTime);
		}
		
		//Look left button
		if(GUI.RepeatButton(new Rect(Screen.width  / 50f,
	                           	     Screen.height / 5.4f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[1])
		   ){
			transform.RotateAround(Vector3.up, - ANGLE * Time.deltaTime);
		}
		
		//Look right button
		if(GUI.RepeatButton(new Rect(Screen.width  / 9.25f,
	                           	     Screen.height / 5.45f,
	                                 Screen.height / 20f,
	                                 Screen.height / 20f),"",guiStyles[3])
		   ){
			transform.RotateAround(Vector3.up, ANGLE * Time.deltaTime);
		}
	}
	
	void DrawMoveCameraControl(){
		
		//Move forward button
		if(GUI.RepeatButton(new Rect(Screen.width  / 16f,
	                           	     Screen.height / 3.075f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[0])
		   ){
			Vector3 direcao = transform.forward * Time.deltaTime * STEP;
			direcao = new Vector3(direcao.x, 0, direcao.z);
			transform.position += direcao;
		}
		
		//Move backward button
		if(GUI.RepeatButton(new Rect(Screen.width  / 16f,
	                           	     Screen.height / 2.17f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[2])
		   ){
			Vector3 direcao = -transform.forward * Time.deltaTime * STEP;
			direcao = new Vector3(direcao.x, 0, direcao.z);
			transform.position += direcao;
		}
		
		//Move to left button
		if(GUI.RepeatButton(new Rect(Screen.width  / 50f,
	                           	     Screen.height / 2.5f,
	                                 Screen.height / 18f,
	                                 Screen.height / 18f),"",guiStyles[1])
		   ){
			Vector3 direcao = -transform.right * Time.deltaTime * STEP;
			direcao = new Vector3(direcao.x, 0, direcao.z);
			transform.position += direcao;
		}
		
		//Move to right button
		if(GUI.RepeatButton(new Rect(Screen.width  / 9.25f,
	                           	     Screen.height / 2.505f,
	                                 Screen.height / 20f,
	                                 Screen.height / 20f),"",guiStyles[3])
		   ){
			Vector3 direcao = transform.right * Time.deltaTime * STEP;
			direcao = new Vector3(direcao.x, 0, direcao.z);
			transform.position += direcao;
		}		
	}
}